package com.demoworld;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;

/**
 * Created by li_wb on 2018/1/24.
 */
public class GsonFactory {
    public static Gson build(final List<String> fieldExclusions, final List<Class<?>> classExclusions, boolean isPretty) {
        GsonBuilder b = new GsonBuilder();
        b.addSerializationExclusionStrategy(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return fieldExclusions == null ? false : fieldExclusions.contains(f.getName());
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return classExclusions == null ? false : classExclusions.contains(clazz);
            }
        });
        if (isPretty) {
            return b.setPrettyPrinting().create();
        }
        return b.create();
    }
}
